package com.simulation.common.utils;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhangxn
 * @date 2024/12/22  15:20
 */
@Slf4j
public class GiteaUtils {

    private static String ADD_USERS = "https://gitea.yanzhiwei.top:31443/api/v1/admin/users";

    private static String ADD_REPO = "https://gitea.yanzhiwei.top:31443/api/v1/user/repos";

    private static String DELETE_REPO = "https://gitea.yanzhiwei.top:31443/api/v1/repos/%s/%s";

    private static String CREATE_REPO_BRANCH = "https://gitea.yanzhiwei.top:31443/api/v1/repos/%s/%s/branches";

    private static String DELETE_REPO_BRANCH = "https://gitea.yanzhiwei.top:31443/api/v1/repos/%s/%s/branches/%s";

    private static String FORK_REPO = "https://gitea.yanzhiwei.top:31443/api/v1/repos/%s/%s/forks";

    private static String DOWNLOAD_REPO = "https://gitea.yanzhiwei.top:31443/api/v1/repos/%s/%s/archive";

    /**
     * 添加人员
     * @param userName
     * @param password
     * @param email
     */
    public static String addGiteaUser(String userName, String password,String email){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("email", email);
        jsonObject.put("full_name", userName);
        jsonObject.put("login_name", userName);
        jsonObject.put("password", password);
        jsonObject.put("username", userName);
        jsonObject.put("must_change_password", false);
        jsonObject.put("restricted", false);
        jsonObject.put("send_notify", true);
        jsonObject.put("source_id", 0);
        //链式构建请求
        String result2 = HttpRequest.post(ADD_USERS)
                .header("Authorization", "token c686770290f51cf5863d9f958980f98bbce2c9b6")//头信息，多个头信息多次调用此方法即可
                .body(jsonObject.toJSONString())//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info("添加gitea用户返回：{}", result2);
        JSONObject jsonObject1 = JSONObject.parseObject(result2);
        return jsonObject1.getString("id");
    }

    /**
     * 添加人员
     * @param userName
     * @param password
     * @param repo
     */
    public static String addGiteaRepo (String userName, String password,String repo){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("auto_init", true);
        jsonObject.put("default_branch", "main");
        jsonObject.put("description", "init");
        jsonObject.put("name", repo);
        jsonObject.put("private", true);
        jsonObject.put("template", true);
        jsonObject.put("trust_model", "default");
        //链式构建请求
//        String url = String.format(ADD_REPO, userName,password);
        String result2 = HttpRequest.post(ADD_REPO)
                .basicAuth(userName,password)
                .body(jsonObject.toJSONString())//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info("添加代码仓库分支返回：{}", result2);
        JSONObject jsonObject1 = JSONObject.parseObject(result2);
        String url = "";
//        if (StringUtils.isNotEmpty(jsonObject1.getString("clone_url"))) {
//            url = jsonObject1.getString("clone_url").replace("https://",
//                    "https://"+userName+":"+password+"@");
//        }
        return jsonObject1.getString("ssh_url");
    }

    /**
     * 创建仓库
     * @param userName
     * @param password
     * @param repo
     * @return
     */
    public static String createGiteaRepo (String userName, String password,String repo){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("auto_init", true);
        jsonObject.put("default_branch", "main");
        jsonObject.put("description", "init");
        jsonObject.put("name", repo);
        jsonObject.put("private", false);
        jsonObject.put("template", true);
        jsonObject.put("trust_model", "default");
        //链式构建请求
//        String url = String.format(ADD_REPO, userName,password);
        String result2 = HttpRequest.post(ADD_REPO)
                .basicAuth(userName,password)
                .body(jsonObject.toJSONString())//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info("添加代码仓库返回：{}", result2);
        JSONObject jsonObject1 = JSONObject.parseObject(result2);
        return jsonObject1.getString("ssh_url"); //ssh_url clone_url(https)
    }

    /**
     * 创建仓库分支
     * @param userName
     * @param password
     * @param repo
     * @return
     */
    public static String createGiteaRepoBranch (String userName, String password,String repo,String branchName){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("new_branch_name", branchName);
        //链式构建请求
        String url = String.format(CREATE_REPO_BRANCH,userName, repo);
        String result2 = HttpRequest.post(url)
                .basicAuth(userName,password)
                .body(jsonObject.toJSONString())//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info("添加代码仓库分支返回：{}", result2);
        return result2;
    }

    /**
     * 删除仓库分支
     * @param userName
     * @param password
     * @param repo
     * @return
     */
    public static String delGiteaRepoBranch (String userName, String password,String repo,String branchName){
        //链式构建请求
        String url = String.format(DELETE_REPO_BRANCH,userName, repo,branchName);
        String result2 = HttpRequest.delete(url)
                .basicAuth(userName,password)
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info("删除代码仓库分支返回：{}", result2);
        return result2;
    }

    /**
     * fork仓库
     * @param userName
     * @param password
     * @param repo
     * @return
     */
    public static String getForkRepoGiteaRepo (String userName, String password,String repo,String owner){
        //链式构建请求
        String url = String.format(FORK_REPO,owner, repo);
        String result2 = HttpRequest.post(url)
                .basicAuth(userName,password)
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info("fork代码仓库分支返回：{}", result2);
        JSONObject jsonObject1 = JSONObject.parseObject(result2);
        return jsonObject1.getString("ssh_url");
    }

    /**
     * 删除仓库
     * @param userName
     * @param password
     * @param repo
     * @return
     */
    public static String delGiteaRepo (String userName, String password,String repo){
        //链式构建请求
        String url = String.format(DELETE_REPO,userName, repo);
        String result2 = HttpRequest.delete(url)
                .basicAuth(userName,password)
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.info("删除代码仓库分支返回：{}", result2);
        return result2;
    }

    /**
     * 下载
     * @param userName
     * @param password
     * @param repo
     * @return
     */
    public static String downloadRepoGitea (String userName, String password,String repo,String owner,String branchName){
        //链式构建请求
        String url = String.format(DOWNLOAD_REPO,owner, repo)+"/"+branchName+".zip";
//        String result2 = HttpRequest.get(url)
//                .basicAuth(userName,password)
//                .timeout(20000)//超时，毫秒
//                .execute().body();
//        log.info("fork代码仓库分支返回：{}", result2);
//        JSONObject jsonObject1 = JSONObject.parseObject(result2);
//        url = url.replace("https://",
//                "https://"+userName+":"+password+"@");
        return url;
    }
}
